package com.zlf.design_pattern.chain_filter.test2;

import com.zlf.design_pattern.chain_filter.test2.filter.Filter;
import com.zlf.design_pattern.chain_filter.test2.param.Request;
import com.zlf.design_pattern.chain_filter.test2.param.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/3.
 */
public class FilterChain implements Filter {

    private List<Filter> filterList = new ArrayList<Filter>();
    private int index = 0;

    public FilterChain addFilter(Filter filter){
        filterList.add(filter);
        return this;
    }

    /**
     * 相当于一个工具
     * 专门加载下一过滤器调用
     *
     * @param request
     * @param response
     * @param filter
     */
    public void doFilter(Request request, Response response, FilterChain filter) {
        if (index == filterList.size()) return;

        Filter f = filterList.get(index++);
        f.doFilter(request, response, filter);
    }
}
